package dao.daoimpl;

import util.DataBase;
import dao.CommandDao;
import bean.Command;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CommandDaoImpl implements CommandDao {


    @Override
    public boolean addCommand(Command command) {
        int articleId=command.getArticleId();
        String time=command.getTime();
        String sql="insert into command_table(command_articleId,command_time) values ('"+articleId+"','"+time+"')";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteCommand(int articleId) {
        String sql="delete from command_table where command_articleId ='"+articleId+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<Command> getAllCommand() throws SQLException {
        ArrayList<Command> commands=new ArrayList<>();
        String sql="select * from command_table";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            int articleId=resultSet.getInt("command_articleId");
            String time=resultSet.getString("command_time");
            commands.add(new Command(articleId,time));
        }
        return commands;
    }
}
